package com.loveprogrammer.handler.support;

import com.alibaba.fastjson2.JSON;
import com.loveprogrammer.command.anotation.TagListener;
import com.loveprogrammer.command.anotation.TopicListener;
import com.loveprogrammer.command.client.ClientMenuTag;
import com.loveprogrammer.command.client.ClientSceneTag;
import com.loveprogrammer.command.client.ClientTopic;
import com.loveprogrammer.command.server.ServerBattleTag;
import com.loveprogrammer.command.server.ServerTopic;
import com.loveprogrammer.console.ConsolePrint;
import com.loveprogrammer.constants.CommonValue;
import com.loveprogrammer.dto.base.StringRequest;
import com.loveprogrammer.dto.battle.BattleFightResult;
import com.loveprogrammer.dto.battle.FightChoose;
import com.loveprogrammer.dto.battle.MapChoose;
import com.loveprogrammer.dto.job.HeroInfo;
import com.loveprogrammer.handler.BaseHandler;
import com.loveprogrammer.network.NetworkClientListener;
import com.loveprogrammer.pojo.StringMessage;
import com.loveprogrammer.utils.ChannelUtils;
import com.loveprogrammer.utils.ScannerInput;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Scanner;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @ClassName MenuHandler
 * @Description TODO
 * @Author admin
 * @Date 2024/2/18 17:37
 * @Version 1.0
 */
@TopicListener(topic = ClientTopic.TOPIC_SCENE)
public class SceneHandler extends BaseHandler {

    public static final Logger log = LoggerFactory.getLogger(SceneHandler.class);

    @TagListener(tag = ClientSceneTag.TAG_SCENE_BATTLE,messageClass = BattleFightResult.class)
    public void portalMenu(ChannelHandlerContext ctx, BattleFightResult result) throws InterruptedException {
        ConsolePrint.publishMessage("战斗开始");

        List<String> fightLogs = result.getFightLogs();
        for (String fightLog : fightLogs) {
            ConsolePrint.publishMessage(fightLog);
        }

        if(result.isWin()){
            ConsolePrint.publishMessage("您胜利了");
        }else {
            ConsolePrint.publishMessage("您失败了");
        }

        AtomicReference<Boolean> quit = new AtomicReference<>(false);
        CompletableFuture.supplyAsync(
                () -> {
                    try {
                        Scanner input = new Scanner(System.in);
                        ConsolePrint.publishMessage("输入q 退出", 1);
                        while (true) {
                            try {
                                String choose = input.nextLine();
                                if ("q".equals(choose)) {
                                    quit.set(true);
                                    break;
                                }
                            } catch (Exception exception) {
                                // exception.printStackTrace();
                                break;
                            } finally {
                                input = new Scanner(System.in);
                                input.reset();
                            }
                        }
                        return true;
                    } catch (Exception exception) {
                        exception.printStackTrace();
                        return false;
                    }
                });

        Thread.sleep(2000);

        if(quit.get()) {
            MapChoose data = new MapChoose();
            data.setMapList(result.getMapList());
            // 返回上一级菜单
            NetworkClientListener.getInstance().forward(ctx,ClientTopic.TOPIC_MENU, ClientMenuTag.TAG_MENU_MAP_CHOOSE, JSON.toJSONString(data));
            return;
        }

        // 继续下一次的
        StringMessage message = StringMessage.create(ServerTopic.TOPIC_BATTLE, ServerBattleTag.TAG_BATTLE_COMMON_FIGHT);
        message.setStatusCode(CommonValue.MSG_STATUS_CODE_SUCCESS);
        FightChoose request = new FightChoose(ClientTopic.TOPIC_SCENE, ClientSceneTag.TAG_SCENE_BATTLE, ClientTopic.TOPIC_MENU,ClientMenuTag.TAG_MENU_MAP_CHOOSE);
        request.setMapId(result.getMapId());
        request.setType(1);
        message.setBody(JSON.toJSONString(request));
        ChannelUtils.pushToServer(ctx.channel(), message);
    }

    /***
     * 选择地图
     * @param ctx
     * @param data
     */
    @TagListener(tag = ClientMenuTag.TAG_MENU_MAP_CHOOSE, messageClass = MapChoose.class)
    public void chooseMaps(ChannelHandlerContext ctx, MapChoose data) {
        //
        ConsolePrint.publishMessage("请选择要进入的地图");
        List<MapChoose.MapItem> mapList = data.getMapList();
        for (int i = 0; i < mapList.size(); i++) {
            ConsolePrint.publishMessage((i+1) + ":" + mapList.get(i).toString(), 1);
        }
        Scanner input = new Scanner(System.in);
        int choose = input.nextInt();
        while(choose < 0 || choose > mapList.size()){
            ConsolePrint.publishMessage("请重新选择", 1);
            choose = input.nextInt();
        }
        MapChoose.MapItem item = mapList.get(choose - 1);
        ConsolePrint.publishMessage("选择的地图为" + item.getName());

        // 继续选择是打怪还是返回
        ConsolePrint.publishMessage("请选择您要进行的操作");
        ConsolePrint.publishMessage("【1.打怪寻宝】  【2.挑战BOSS】  【3.返回】");
        choose = ScannerInput.inputInt(1, 3, 3);
        if(choose != 3) {
            switch (choose) {
                case 1 :
                    // 战斗
                    StringMessage message = StringMessage.create(ServerTopic.TOPIC_BATTLE, ServerBattleTag.TAG_BATTLE_COMMON_FIGHT);
                    message.setStatusCode(CommonValue.MSG_STATUS_CODE_SUCCESS);
                    FightChoose request = new FightChoose(ClientTopic.TOPIC_SCENE, ClientSceneTag.TAG_SCENE_BATTLE, ClientTopic.TOPIC_MENU,ClientMenuTag.TAG_MENU_MAP_CHOOSE);
                    request.setMapId(item.getId());
                    request.setType(1);
                    message.setBody(JSON.toJSONString(request));
                    ChannelUtils.pushToServer(ctx.channel(), message);
                    break;
                case 2:
                    // 战斗
                    ConsolePrint.publishMessage("暂未开放，敬请期待", 1);
                    // break;
                default:
                    // 返回上一页
                    NetworkClientListener.getInstance().forward(ctx,ClientTopic.TOPIC_MENU, ClientMenuTag.TAG_MENU_MAP_CHOOSE, JSON.toJSONString(data));
            }
        }
        else {
            // 返回上一页
            NetworkClientListener.getInstance().forward(ctx,ClientTopic.TOPIC_MENU, ClientMenuTag.TAG_MENU_MAP_CHOOSE, JSON.toJSONString(data));
        }
    }


    @TagListener(tag = ClientSceneTag.TAG_SCENE_HERO, messageClass = HeroInfo.class)
    public void heroInfo(ChannelHandlerContext ctx, HeroInfo data) {
        ConsolePrint.publishMessage("英雄属性如下");
        ConsolePrint.publishMessage(data.prettyPrint());


        ConsolePrint.publishMessage("英雄装备如下");


        ConsolePrint.publishMessage("请选择操作");
        ConsolePrint.publishMessage("【1.根据系统推荐更换装备】  【2.查看已有装备,自选装备更换】 【3.卖掉装备】", 1);
        ConsolePrint.publishMessage("【4.合成装备】   【5.查看共享装备】  【6.返回】", 1);
        ConsolePrint.publishMessage("请选择：");
        int choose = ScannerInput.inputInt(1, 6, 6);
        if (choose != 6) {
            switch (choose) {
                case 1:
                    ConsolePrint.publishMessage("暂未开放");
                    break;
                case 2:
                    ConsolePrint.publishMessage("暂未开放");
                    break;
                case 3:
                    ConsolePrint.publishMessage("暂未开放");
                    break;
                case 4:
                    ConsolePrint.publishMessage("暂未开放");
                    break;
                case 5:
                    ConsolePrint.publishMessage("暂未开放");
                    break;
                default:
                    ConsolePrint.publishMessage("暂未开放");
                    break;
            }
        }

        // 返回上一级菜单 主菜单
        StringRequest msg = new StringRequest();
        NetworkClientListener.getInstance().forward(ctx,ClientTopic.TOPIC_MENU, ClientMenuTag.TAG_MENU_PORTAL, JSON.toJSONString(msg));
    }
}
